<?php # Script 3.9 - calculator04.php

$page_title = 'Widget Cost Calculator';
include('includes/header.html');
?>
	<div id="wrap">
		<div class="navbar navbar-inverse">
			<div class="navbar-inner">
				<div class="container">
					<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
						<span class="icon-bar"></span>
						<span class="icon-bar"></span>
						<span class="icon-bar"></span>
					</a>
					<a class="brand" href="#">Your Website</a>
					<div class="nav-collapse collapse">
						<ul class="nav">
							<li class=""><a href="index.php">홈</a></li>
							<li class="active"><a href="calculator04.php">계산기</a></li>
							<li class=""><a href="dateform.php">날짜 폼</a></li>
							<li class=""><a href="#">link four</a></li>
							<li class=""><a href="#">link five</a></li>
						</ul>
					</div>
				</div>
			</div>
		</div>
		<div class="container">
			<div class="page-header">
    	<!-- Start of the page-specific content. -->

<?php
/* This function calculates a total
 * and then prints the results. 
 * The $tax argument is optional (it has a default value). */
function calculate_total($qty, $cost, $tax = 5) {
	
	$total = ($qty * $cost);
	$taxrate = ($tax / 100); // Turn 5% into .05.
	$total += ($total * $taxrate); // Add the tax.
	
	// Print the results:
	echo '<p class="text-info">The total cost of purchasing '. $qty . ' widget(s) at $' . number_format($cost, 2) . ' each, including a tax rate of ' . $tax . '%, is $' . number_format($total, 2) . '.</p>';
	
} // End of function.

// Check for form submission:
if (isset($_POST['submitted'])) {
	
	// Minimal form validation:
	if (is_numeric($_POST['quantity']) && is_numeric($_POST['price']) ) {
		
		// Print the heading:
		echo '<h1>Total Cost</h1>';
									
		// Call the function:
		if (is_numeric($_POST['tax'])) {
			calculate_total($_POST['quantity'], $_POST['price'], $_POST['tax']);
		} else {
			calculate_total($_POST['quantity'], $_POST['price']);
		}
		
	} else { // Invalid submitted values.
		echo '<h1>Error!</h1>
		<p class="error">Please enter a valid quantity, price, and tax.</p>';
	}

} // End of main isset() IF.

// Leave the PHP section and create the HTML form:
?>

        	<h1>Widget Cost Calculator</h1>
        	<form action="calculator04.php" method="post" class="form-horizontal">

				<fieldset>
					<div class="control-group">
						<label class="control-label">Quantity:</label>
						<div class="controls">
							<input type="text" class="input-xlarge" name="quantity" size="5" maxlength="5" value="<?php if(isset($_POST['quantity'])) echo $_POST['quantity']; ?>">
						</div>
					</div>
					<div class="control-group">
						<label class="control-label">Price:</label>
						<div class="controls">
							<input type="text" class="input-xlarge" name="price" size="5" maxlength="10" value="<?php if(isset($_POST['price'])) echo $_POST['price']; ?>">
						</div>
					</div>
					<div class="control-group">
						<label class="control-label">Tax(%):</label>
						<div class="controls">
							<input type="text" class="input-xlarge" name="tax" size="5" maxlength="5" value="<?php if(isset($_POST['tax'])) echo $_POST['tax']; ?>"> (optional)
						</div>
					</div>
					<div class="form-actions">
                        <button type="submit" class="btn btn-primary btn-large">Calculate!</button>
                        <input type="hidden" name="submitted" value="1">
                    </div>
				</fieldset>
			</form>
<?php
include('includes/footer.html');
?>